package com.moefor.clover.service;

import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.Resource;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.moefor.clover.dao.UserDao;

@Service
public class SecurityUserService implements UserDetailsService{
	@Resource
	private UserDao userDao;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		UserDetails userDetails = null;
		com.moefor.clover.model.User user = userDao.get(username);
		if(user == null){
			throw new UsernameNotFoundException(username + "Not Found!");
		}
		Collection<SimpleGrantedAuthority> authList = new ArrayList<SimpleGrantedAuthority>();
		authList.add(new SimpleGrantedAuthority("ROLE_USER"));
		userDetails = new User(username, user.getPassword(), true, true, true, true, authList);
		return userDetails;
	}
}
